1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 10-Jan-2005
6    */
7   package ca.uhn.cache.internal.impl;
8   
9   import org.jmock.Mock;
10  import org.springframework.context.support.ClassPathXmlApplicationContext;
11  
12  import ca.uhn.cache.helper.CacheCleanerHolder;
13  import ca.uhn.cache.internal.ICacheCleaner;
14  import ca.uhn.cache.util.SpringTestCaseUtils;
15  
16  /***
17   * Tests the scheduled execution of the <code>CacheCleaner.evictStaleChunks()</code> method. 
18   * 
19   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
20   * @version $Revision: 1.2 $ updated on $Date: 2005/01/25 22:05:32 $ by $Author: aguevara $
21   */
22  public class ScheduledCacheCleanerTest extends ca.uhn.cache.jmock.MockObjectTestCase {
23      
24      private static final String CACHE_CLEANER_BEAN = "myCacheCleaner";
25      
26      private Mock myCacheCleanerMock;
27      private ICacheCleaner myCacheCleaner;
28      
29      /***
30       * Constructor for CacheCleanerTest.
31       * @param theName ... 
32       */
33      public ScheduledCacheCleanerTest(String theName) {
34          super(theName);
35      }
36      
37      /*
38       * @see TestCase#setUp()
39       */
40      protected void setUp() throws Exception {
41          super.setUp();
42          
43          ClassPathXmlApplicationContext appContext = 
44              new ClassPathXmlApplicationContext( 
45                      SpringTestCaseUtils.packageToPath( HibernateChunkStoreTest.class.getPackage() ) +
46                      "/scheduled-cache-cleaner.xml");
47  
48          myCacheCleanerMock = new Mock( ICacheCleaner.class );
49          myCacheCleaner = (ICacheCleaner) myCacheCleanerMock.proxy(); 
50          
51          CacheCleanerHolder holder = (CacheCleanerHolder) appContext.getBean( CACHE_CLEANER_BEAN );
52          holder.setHoldedCacheCleaner( myCacheCleaner );
53      }
54      
55      /***
56       * {@inheritDoc}
57       */
58      protected void tearDown() throws Exception {
59          super.tearDown();
60      }
61      
62      /***
63       * call CacheCleaner.evictStaleChunks() will be executed 5 times in the next 5 seconds.
64       */
65      public void test1() {
66          myCacheCleanerMock.expects( count( 5 )  )
67              .method( "evictStaleChunks" )
68              .withNoArguments();
69          
70          try { Thread.sleep( 5 * 1000 ); } catch (InterruptedException e) {}
71          
72      }
73  
74  }